package org.gjy.m8.concurrent;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-07-19 09:35:48
 */
public class AddTest {

    private static final Integer SIZE = 10_000;
    static Integer count = 0;
    static Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < SIZE; i++) {
                synchronized (lock) {
                    count++;
                }
            }
        });
        thread.start();

        for (int i = 0; i < SIZE; i++) {
            synchronized (lock) {
                count++;
            }
        }
        thread.join();

        System.out.println("count = " + count);
    }

    private static void t1() throws InterruptedException {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < SIZE; i++) {
                count++;
            }
        });
        thread.start();

        for (int i = 0; i < SIZE; i++) {
            count++;
        }
        thread.join();

        System.out.println("count = " + count);
    }

}
